package baidu.com.matedemo.dlp;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.baidu.duer.libcore.util.ToastUtil;
import com.baidu.duer.smartmate.protocol.dlp.bean.tts.TTSInfoPayload;
import com.baidu.duer.smartmate.proxy.controller.ControllerManager;
import com.baidu.duer.smartmate.proxy.controller.DCSDataObserver;
import com.google.gson.Gson;

import baidu.com.matedemo.base.BaseFragment;
import baidu.com.matedemo.R;
import baidu.com.matedemo.DemoApp;
import baidu.com.matedemo.util.DemoUtils;

/**
 * Created by niufei01 on 2017/10/23.
 */

public class TTSInfoDemo extends BaseFragment implements DCSDataObserver<TTSInfoPayload> {

    ControllerManager controllerManager;
    ViewHolder viewHolder = null;

    @Override
    public View onCreateContentViewCompat(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.tts_info, viewGroup, false);
    }

    @Override
    public void onContentViewCreated(final View view) {
        controllerManager = DemoApp.getInstance().getCurreDuerDevice().getControllerManager();
        controllerManager.registerTTSInfoObserver(this);
        viewHolder = new ViewHolder(view);
        viewHolder.getstatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                controllerManager.getTTSInfoStatus();
            }
        });

        viewHolder.updateTts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String info = viewHolder.edittext.getText().toString();
                if (TextUtils.isEmpty(info)) {
                    ToastUtil.toastWarnning(getMActivity(),"内容不能为空，请先获取音色信息");
                    return;
                }
                TTSInfoPayload payload = new Gson().fromJson(info, TTSInfoPayload.class);
                controllerManager.updateTTSInfo(payload);
            }
        });
    }

    @Override
    public void onDataChanaged(String event, TTSInfoPayload message) {
        viewHolder.edittext.setText(DemoUtils.toFormatJson(message));
    }

    @Override
    public void onDestroy() {
        if (controllerManager != null) {
            controllerManager.unregisterTTSInfoObserver(this);
        }
        super.onDestroy();
    }

    public static class ViewHolder {
        public View rootView;
        public EditText edittext;
        public Button getstatus;
        public Button updateTts;
        public LinearLayout buttonLayout;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.edittext = (EditText) rootView.findViewById(R.id.edittext);
            this.getstatus = (Button) rootView.findViewById(R.id.getstatus);
            this.updateTts = (Button) rootView.findViewById(R.id.updateTts);
            this.buttonLayout = (LinearLayout) rootView.findViewById(R.id.button_layout);
        }
    }
}
